package com.whsr.readservice.controller;

import com.github.pagehelper.PageInfo;
import com.whsr.readservice.dto.ArticleRequest;
import com.whsr.readservice.service.ArticleService;
import com.whsr.readservice.util.ResultUtil;
import com.whsr.readservice.vo.Article;
import com.whsr.readservice.vo.PageConfigGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/server/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 单表----条件查询---作品信息列表
     * @param article
     * @return
     */
    @RequestMapping(value = "/selectArticles",method = RequestMethod.POST)
    public List<Article> selectArticles(@RequestBody ArticleRequest article){
        return articleService.selectArticlesByArticle(article);
    }

    /**
     * 多表联合----条件查询---作品信息加作品类型列表
     * @param article
     * @return
     */
    @RequestMapping(value = "/getArticlesByUnion",method = RequestMethod.POST)
    public PageInfo<ArticleRequest> getArticlesByUnion(@RequestBody ArticleRequest article){
        return articleService.getArticlesByUnion(article);
    }

    /**
     * 根据bookId 删除作品
     * @param bookId
     * @return
     */
    @RequestMapping(value = "/deleteArticle/{bookId}/{dataFlag}",method = RequestMethod.GET)
    public int changeArticle(@PathVariable Integer bookId, @PathVariable Integer dataFlag){
        return articleService.changeArticleByBookId(bookId, dataFlag);
    }


    /**
     * @author chatwin
     * @param name 页面传过来的书名
     * @return
     */
    @RequestMapping(value = "/queryArtByName/{name}",method = RequestMethod.POST)
    @ResponseBody
    public Object queryArtByName(@PathVariable String name){
        Map<String, Object> resultMap = new HashMap<>();
        try{
            List<Map<String, Object>> list = this.articleService.queryArtByName(name);
            resultMap.put("errno", 0);
            resultMap.put("msg", "configGroup query success");
            resultMap.put("data", list);
        }catch (Exception e){
            resultMap.put("errno", 0);
            resultMap.put("msg", "configGroup query err");
            resultMap.put("data", null);
        }
        return resultMap;
    }

    /**
     * @author chatwin
     * @param bookId 页面传过来的书号
     * @return
     */
    @RequestMapping(value = "/queryArtByApp/{bookId}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> queryArtByApp(@PathVariable Integer bookId){
        try {
            return articleService.queryArtByApp(bookId);
        } catch (Exception e) {
            return ResultUtil.getResultByError();
        }
    }

}
